
import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PImage;
import tuioZones.DropArea;
import tuioZones.GamingArea;
import tuioZones.GamingZone;
import tuioZones.Puzzle;
import tuioZones.TouchPoint;
import tuioZones.TouchZone;


// NOTE: You need to manually change the package name on the next line and in the line within the main function.
// This will be automated in future versions once my Netbeans powers have developed...



/**
 *
 */
public class Applet extends PApplet {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PApplet.main(new String[]{"netbeans_processing_template.Main"});
    }
    
    GamingArea area;
    PImage a;
    PImage b;

    public void setup() {
        size(screen.width,screen.height);
        //size(1024,768);
        // create a draggable, throwable, and scalable zone for a photo
            area=new GamingArea(this);
        noFill();
        smooth();
    }

    public void draw() {
        background(0);

          GamingZone gz = area.getGamingZone();
          stroke(gz.getR(), gz.getG(), gz.getB());
          strokeWeight(10);
          strokeJoin(ROUND);
          noFill();
          rect(gz.getX(), gz.getY(), gz.getWidth(), gz.getHeight());

          for(int i=0; i < area.getDropAreas().length; i++) {
            DropArea d = area.getDropAreas()[i];
            stroke(d.getR(),d.getG(),d.getB());
            strokeWeight(10);
            strokeJoin(ROUND);
            noFill();
            rect(d.getX(),d.getY(), d.getWidth(), d.getHeight());
            //display all finished puzzles
            ArrayList<Puzzle> puzzles = d.getFinishedPuzzles();

            if (puzzles.size()!=0) {
                int counter = -1;

                for (Puzzle p : puzzles) {
                    counter++;
                    PImage image = p.getCA().getBgImage();
                    PImage image2 = p.getCB().getBgImage();

                    translate(d.getTranslationPoint().getX(), d.getTranslationPoint().getY());
                    rotate(radians(d.getAngle()));
                    image(image, ((counter)/3)*d.getPuzzleWidth(), (counter%3)*d.getPuzzleHeight(), d.getPuzzleWidth()/2, d.getPuzzleHeight());
                    image(image2, ((counter)/3)*d.getPuzzleWidth() + d.getPuzzleWidth()/2, (counter%3)*d.getPuzzleHeight(), d.getPuzzleWidth()/2, d.getPuzzleHeight());
                    rotate(-radians(d.getAngle()));
                    translate(-d.getTranslationPoint().getX(), -d.getTranslationPoint().getY());

                    // -1, because we start with 0
                    if (counter == d.getMaxPuzzles()-1) {

                        if (d.getR2()!=0)
                            d.setR2(d.getR2()-3);
                        if (d.getG2()!=0)
                            d.setG2(d.getG2()-3);
                        if (d.getB2()!=0)
                            d.setB2(d.getB2()-3);

                        fill(d.getR2(),d.getG2(),d.getB2());
                        rect(d.getX(), d.getY(), d.getWidth(), d.getHeight());
                        noFill();
                        
                        if ((d.getR2()<=0)&&(d.getG2()<=0)&&(d.getB2()<=0)) {
                            d.resetInsideRGB();
                            d.removeAllPuzzles();
                        }                                                
                    }                    
                }                
            }
          }

          for(int i=0; i < area.getTouchZones().size(); i++) {
            TouchZone tz = area.getTouchZones().get(i);
            noStroke();
            noFill();
            if(tz.getCard() == null) {
              // fuck
            } else {
              //tz.setAngle(180);              
              if (tz.getPuzzle()==null) {
                  translate(tz.getX(), tz.getY());
                  rotate(radians(tz.getAngle()));
                  PImage img = tz.getCard().getBgImage();
                  image(img, 0, 0);
                  rotate(radians(-tz.getAngle()));
                  translate(-tz.getX(), -tz.getY());

                  if(tz.getRotationPoint() != null) {
                    stroke(123,0,0);
                    ellipse(tz.getRotationPoint().getX(),tz.getRotationPoint().getY(),20,20);
                  }
              } else {                  
                  tz.setWidth(GamingArea.CARD_WIDTH*2);                  
                  translate(tz.getX(), tz.getY());
                  rotate(radians(tz.getAngle()));                                    
                  PImage img1 = tz.getPuzzle().getCA().getBgImage();                  
                  PImage img2 = tz.getPuzzle().getCB().getBgImage();
                  image(img1, 0, 0);
                  image(img2, GamingArea.CARD_WIDTH,0);
                  rotate(radians(-tz.getAngle()));
                  translate(-tz.getX(), -tz.getY());
                  
                  if(tz.getRotationPoint() != null) {
                      stroke(123,0,0);
                      ellipse(tz.getRotationPoint().getX(),tz.getRotationPoint().getY(),20,20);
                    }
              }              
            }
            /*
            fill(255,0,0);
            ellipse(tz.getCoordA().getX(),tz.getCoordA().getY(),10,10);
            fill(0,255,0);
            ellipse(tz.getCoordB().getX(),tz.getCoordB().getY(),10,10);
            fill(0,0,255);
            ellipse(tz.getCoordC().getX(),tz.getCoordC().getY(),10,10);
            fill(123,123,123);
            ellipse(tz.getCoordD().getX(),tz.getCoordD().getY(),10,10);

            */
          }

          for(int i=0; i < area.getTouchPoints().size(); i++) {
            TouchPoint tp = area.getTouchPoints().get(i);
            noStroke();
            fill(100,100,100);
            ellipse(tp.getX(), tp.getY(), 20,20);
          }
    }
}
